#include <jni.h>
#include <android/log.h>
#include "JniUtil.h"
#include "tree.h"
#include "util.h"

using namespace NS_TIEC;

class JVisitor : public Visitor<int, int>{
public:
  JVisitor(JNIEnv* env, jobject java_obj) : env_(env), java_obj_(java_obj) {
    jclass klass = env->FindClass("com/tiecode/compiler/api/tree/Visitor");
    INIT_JMETHOD(klass, VisitIdentifierTree, visit, "(Ljava/lang/String;)V")
  }

  int VisitIdentifier(IdentifierTree* tree, int param) override {
    env_->CallVoidMethod(java_obj_, JMETHOD(VisitIdentifierTree), env_->NewStringUTF("hhhhhhh"));
    return 0;
  }

private:
  JNIEnv* env_;
  jobject java_obj_;
  DEF_JMETHOD(VisitIdentifierTree)
};

#include <iostream>
template<typename Param, typename Result>
static void TestVisit(tiec::Visitor<Param, Result>* visitor) {
  auto start = TimeUtil::MilliTime();
  for (int i = 0; i < 200 * 400 * 6; ++i) {
    visitor->VisitIdentifier(nullptr, 0);
  }
  auto end = TimeUtil::MilliTime();
  __android_log_print(ANDROID_LOG_INFO, "TIEC", "CallJavaMethodTime: %ld ms", (end - start));
}